#include <pthread.h>
#include <iostream>
#include <unistd.h>

using namespace std;


class Request
{
public:
    Request(int start, int end, const string& threadname)
        :_start(start)
        ,_end(end)
        ,_threadname(threadname){}
public:
    int _start;
    int _end;
    string _threadname;
};

class Response
{
public:
    Response(int result, int errorcode)
        :_result(result)    
        ,_errorcode(errorcode){}

public:
int _result;
int _errorcode;
};

void *SumCount(void* arg)
{
    Request* req = static_cast<Request*>(arg);
    Response* rsp = new Response(0, 0);

    for (int i = req->_start; i <= req->_end; i++)
    {
        rsp->_result += i;
    }

    delete req;     //可以在内部释放内存
    return (void*)rsp;

}


int main()
{
    pthread_t tid;
    Request* req = new Request(1, 100, "thread_1");
    int n = pthread_create(&tid, nullptr, SumCount, (void*)req);   //函数原型：int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
    
    void* result = nullptr;
    pthread_join(tid, &result);

    Response* rsp = static_cast<Response*>(result);
    cout << rsp->_result << endl;

    delete rsp;     //使用完指针之后，释放内存
    return 0;
}